<template>
	<view>
		<view class="head">
			<view class="head_left" @tap="back()">
				<image src="../../../static/wode/return.png" mode=""></image>
				<text>返回</text>
			</view>
			<view class="head_mid">
				国家地区
			</view>
			<view class="head_left">

			</view>
		</view>
		<view class="countryBox">
			<view class="inputBox">
				<input type="text" v-model="countryName" placeholder="搜索" @input="searchCountry" />
			</view>
			<view class="marginTop">
				<view v-if="isShow" class="companyBox height fontSize" v-for="(item,index) in countryLists" :key="index" v-text="item.Name"
				 @tap="selsectCountry(item.Name)"></view>
				<!-- <view v-if="isDisplay" class="companyBox height fontSize"  v-text="countrys" @tap="selsectCity()"></view> -->
			</view>

		</view>
	</view>
</template>

<script>
	import {
		Location
	} from '../../../common/worldCity.js'
	export default {
		data() {
			return {
				countryName: "",
				countryLists: Location.CountryRegion, //国家列表
				isShow: true,
				isDisplay: false,
				countrys: "",
				isNativePlace: false, //判断是选择籍贯还是所在地
				just: false,
				ishometowm: false, //故乡
				isJiguan: false,
				justCountry: false, //只选择国家
			};
		},
		mounted() {
			console.log(this.countryLists)
		},
		onLoad(option) {
			console.log(option)
			this.isNativePlace = option.isNativePlace //从上一个页面传入判断是选择籍贯还是所在地
			if (option.isHometown != undefined) {
				this.isHometown = option.isHometown
			}
			if (option.isJiguan != undefined) {
				this.isJiguan = option.isJiguan
			}
			if (option.justCountry != undefined) {
				this.justCountry = true
			}

		},
		methods: {
			searchCountry() {
				console.log(1)
				if (this.countryName == '') {
					this.isShow = true
				} else {
					for (var i = 0; i < this.countryLists.length; i++) {
						if (this.countryName == this.countryLists[i].Name) {
							this.isShow = false
							this.isDisplay = true
							this.countrys = this.countryLists[i].Name
						}
					}
				}
			},
			selsectCountry(country) { //根据国家查询城市或省份
				console.log(country)
				if (this.justCountry == true) {
					uni.setStorageSync('countryName', country)
					uni.navigateBack({
						delta: 1
					})
				} else {
					for (var i = 0; i < this.countryLists.length; i++) {
						if (country == this.countryLists[i].Name) {
							if (this.countryLists[i].State || this.countryLists[i].City) { //如果有省份或者城市
								uni.navigateTo({
									url: "../provinceLists/provinceLists?country=" + country + '&isNativePlace=' + this.isNativePlace +
										'&isHometown=' + this.isHometown + '&isJiguan=' + this.isJiguan
								})
							} else { //没有就保存
								if (this.isNativePlace == 'true') { //返回选择的国家并返回是籍贯还是所在地
									uni.removeStorageSync('countryName');
									uni.removeStorageSync('province');
									uni.removeStorageSync('city');

									uni.setStorageSync('countryName', country)

									var me = this;
									var data = {
										user_id: uni.getStorageSync('user_id'),
										hometown: country
									}
									if (this.isHometown) {
										this.myAjaxNewPost({
											model: 'user',
											controllerName: 'userInfo',
											actionName: 'update',
											data: data,
											successBack: function(res) {
												console.log(res, '更新故乡');
											}
										})
									}
									if (this.isJiguan) {
										this.myAjaxNewPost({
											model: 'user',
											controllerName: 'overseasIdentity',
											actionName: 'update',
											data: {
												user_id: uni.getStorageSync('user_id'),
												native_place: me.country
											},
											successBack: function(res) {
												console.log(res, '更新籍贯');
											}
										})
									}
								} else { //所在地
									uni.removeStorageSync('guojia');
									uni.removeStorageSync('shengfen');
									uni.removeStorageSync('chengshi');

									uni.setStorageSync('guojia', country)
									var data = {
										country: '',
										user_id: '',
										city: ''
									}
									data.country = uni.getStorageSync('guojia', country)
									data.user_id = uni.getStorageSync('user_id')
									this.myAjaxNewPost({
										model: 'user',
										controllerName: 'userInfo',
										actionName: 'update',
										data: data,
										successBack: function(res) {
											console.log(res);
										}
									})

								}
								uni.navigateBack({
									delta: 1
								})
							}
						}
					}
				}



			},
			back() { //返回
				uni.navigateBack({
					delta: 1
				})
			},
		}
	}
</script>

<style scoped>
	.head {
		width: 100%;
		display: flex;
		justify-content: space-between;
		height: 162upx;
		background: #ffffff;
		padding: 0 24upx;
		z-index: 200;
		position: fixed;
	}

	.head image {
		width: 20upx;
		height: 36upx;
		margin-right: 9upx;
	}

	.head_left {
		height: 82upx;
		width: 106upx;
		display: flex;
		align-items: center;
		line-height: 82upx;
		color: #666666;
		font-size: 32upx;
		padding-top: 70upx;
	}

	.head_right {
		width: 150upx;
	}
	.head_mid{
		padding-top: 90upx;
	}

	.companyBox {
		display: flex;
		align-items: center;
		border-bottom: 1upx solid #e4e4e4
	}

	.companyBox.height {
		height: 90upx;
		background-color: white;
		padding: 0 40upx;
		box-sizing: border-box;
	}

	.fontSize {
		font-size: 30upx;
	}

	.marginTop {
		position: relative;
		top: 110upx;
		z-index: 11;
		padding-top: 136upx;

	}

	.inputBox {
		width: 100%;
		height: 90upx;
		display: flex;
		position: fixed;
		padding: 20upx 40upx 0 40upx;
		box-sizing: border-box;
		border-bottom: 1rupx solid #e4e4e4;
		z-index: 111;
		text-align: center;
		background: #f5f5f5;
		margin-top: 163upx;
	}

	.inputBox input {
		width: 100%;
		height: 50upx;
		font-size: 30upx;
		border: 1upx solid #E4E4E4;
		border-radius: 50upx;
		padding-left: 20upx;
	}
</style>
